package com.hhq.usercenter.security;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hhq.usercenter.domain.model.MenuModel;
import com.hhq.usercenter.mapper.MenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>Description: some description </p>
 *
 * @author : xiaodong.yang
 * @date : 2024/1/9 18:16
 */
@Component
public class DynamicSecurityService {

    @Autowired
    private MenuMapper menuMapper;

    public Map<String, ConfigAttribute> loadResourceSource() {
        LambdaQueryWrapper<MenuModel> queryWrapper = Wrappers.lambdaQuery(MenuModel.class);
        List<MenuModel> menuModels = menuMapper.selectList(queryWrapper);
        Map<String, ConfigAttribute> map = new ConcurrentHashMap<>();
        for (MenuModel menuModel : menuModels) {
            map.put(menuModel.getApiUrl(), new org.springframework.security.access.SecurityConfig(menuModel.getId() + ":" + menuModel.getName()));
        }
        return map;
    }
}
